<?php
namespace Admin\Controller;
use Think\Controller;
class GroupController extends AdminController
{
    public function index()
    {
        $this->assign('title', '团购列表');
        $Model = new \Think\Model();
        $list = $Model->
        field(array('g.id as gid,s.id as sid ,s.begin_time,s.end_time,g.price,g.name as gname,s.is_show as g_is_show,s.gprice,s.limit_num,s.user_num'))->table('shopping_group as s,shopping_goods as g')->where("g.id=s.goods_id")->select();
//        dump($list);die;

        $ms = M('group');
        $res = $ms->select();
        $today = mktime();//即时时间戳
        foreach ($res as $k => $val) {
            $end_time = strtotime($res[$k]['end_time']);
            $id = $res[$k]['id'];
            if ($end_time <= $today) {
                $data['is_lose'] = 0;//0过期
                $res1 = $ms->where("id='$id'")->save($data);
            } else {
                $data['is_lose'] = 1;//1未过期
                $res1 = $ms->where("id='$id'")->save($data);
            }
        }
        $res = $ms->select();
//        $this->assign('today',$today);
        $this->assign('list', $list);
        $this->display('Group/index');
    }

    public function add(){
        $this->assign('title','添加团购');
        //查询开启秒杀活动的goods_id
        $goods_id_seck = M('goods')->where("goods_type=2 AND is_show=1")->select();

        $this->assign('goods',$goods_id_seck);//添加的id
        $this->display('Group/add');
    }
    public function adddo(){
        $goods_id = $_POST['id'];//商品id
        $data['goods_id']=$_POST['id'];//团购用户人数
        $data['user_num']=$_POST['user_num'];//团购用户人数
        $data['gprice']=$_POST['gprice'];//商品团购价格
        $data['limit_num']=I('post.limit_num',0,'int');//限购数量
        $data['is_show'] = $_POST['is_show'];//是否显示
        $data['begin_time'] = $_POST['begin_time'];//开始时间
        $data['end_time'] = $_POST['end_time'];//结束时间
        //判断开始时间/结束时间是否设置错误
        $a = strtotime($_POST['begin_time']);
        $b = strtotime($_POST['end_time']);
        if($b<$a){
            $this->error('结束时间设置错误,请重新设置',U('Group/index'));
        }
        $r1 =M('group')->where("goods_id='$goods_id'")->find();
        if($r1){
            //存在于秒杀列表时
            $this->error('商品已在团购列表',U('Group/index'));
        }else{
            //不存在于秒杀列表时
            //判断数量
            $r2 =M('goods')->where("id='$goods_id'")->find();
            $sj_num = $r2['num'];
            $num = I('post.limit_num',0,'int');
            if($num>$r2['num']){
                $this->error("秒杀数量大于总数量,最大数量为:$sj_num",U('Group/add'));
            }else{
                //数量符合时
                //写入数据库
                $res = M('group')->add($data);
                $this->success('添加成功',U('Group/index'));
            }
        }
    }
    public function del(){
        $id = I('get.id',0,'int');
//        dump($id);die;
        if(M('group')->delete($id)>0){
            $this->success('删除团购成功',U('Group/index'));
        }else{
            $this->error('删除团购失败',U('Group/index'));
        }


    }
    public function order(){
        $this->assign('title','团购订单');
        $list = M('grouporder')
            ->field("go.id,go.order_id,go.create_time,go.is_ok,go.is_status,u.name uname,g.name gname,go.price,go.is_pay,go.is_deliver,go.is_status")
            ->alias('go')
            ->join('shopping_user u ON go.user_id= u.id')
            ->join('shopping_goods g ON go.goods_id= g.id')
            ->select();

//        dump($list);die;
        $this->assign('list',$list);
        $this->display('Group/order');
    }
    public function is_open(){
        $ms = M("grouporder"); // 实例化User对象
        $b = I('get.sub')==0?'1':'1';
        $data['is_deliver'] = $b;
        $id = I('get.id',0,'int');
        //判断是否付款,付款则发货
        $res = $ms->where("id=$id and is_status=1")->find();
//        dump($res);die;
        if($res == null){
            //已拼团的
            $this->error('拼团未成功,不能发货',U('Group/order'));

        }else{
            //未拼团成功的
            $ms->where("id='$id'")->save($data);
            header("Location:".$_SERVER['HTTP_REFERER']);//改变后返回

        }



    }


}